﻿// 用于判断是否是新建合并请求页面的正则
const isGitLabMergeRequestPage = /^http:\/\/gitserver\.zt\.com\/.+merge_requests\/new\?/;
// 用于获取源分支的正则
const regGetSourceBranch = /(?:.+\[source_branch\]=)([^&]*)(?:.*)/;
// 注意，必须设置了run_at=document_start 此段代码才会生效
document.addEventListener('DOMContentLoaded', function () {
  const href = decodeURI(location.href);
  if (isGitLabMergeRequestPage.test(href)) {
    // 标题
    const titleInputDom = document.querySelector("#merge_request_title");
    if (regGetSourceBranch.test(href)) {
      // 将分支号赋值给标题
      titleInputDom.value = href.replace(regGetSourceBranch, "$1");
    }
    // 合并人
    const assigneeInputDom = document.querySelector("#merge_request_assignee_id");
    // 设置合并人为 root
    assigneeInputDom.value = 1;
    // 提交
    document.querySelector(".qa-issuable-create-button").click()
  }

  function camel(str = '') {
    str = str.trim().toLowerCase();
    return {
      large: str.replace(/[- ]+[a-zA-z]/g, a => a[a.length - 1].toUpperCase()).replace(/./, a => a.toUpperCase()),
      small: str.replace(/[- ]+[a-zA-z]/g, a => a[a.length - 1].toUpperCase()).replace(/./, a => a.toLowerCase()),
      self: str.replace(/ +/g, '-'),
      _large: str.replace(/[- ]+[a-zA-z]/g, a => `_${a[a.length - 1].toUpperCase()}`).replace(/./, a => a.toUpperCase()),
      _self: str.replace(/[- ]+/g, '_'),
    }
  }

  if (location.host === 'fanyi.baidu.com') {
      const wrap = document.createElement('div');
      wrap.id = `dev-tool-camel`;
      wrap.innerHTML  = `
      <input type="text">
      <ul>
          <li><span>self</span><input type="text" readonly="readonly" value=""></li>
          <li><span>large</span><input type="text" readonly="readonly" value=""></li>
          <li><span>small</span><input type="text" readonly="readonly" value=""></li>
          <li><span>_large</span><input type="text" readonly="readonly" value=""></li>
          <li><span>_self</span><input type="text" readonly="readonly" value=""></li>
      </ul>
  `
      const style = document.createElement('style');
      style.innerHTML = `
      #dev-tool-camel{
          position: fixed;
          width: 200px;
          top: 20px;
          right: 20px;
          overflow: hidden;
          display: flex;
          flex-direction: column;
          background-color: #fff;
          box-shadow: 0 2px 2px #999;
          padding: 10px;
          z-index: 2147483648;
      }
      #dev-tool-camel > input{
  
      }
      #dev-tool-camel > ul {
          padding: 10px;
      }
      #dev-tool-camel > ul > li{
          display: flex;
          padding: 4px 0;
      }
      #dev-tool-camel > ul > li span{
          flex: 0 0 60px;
      }
      #dev-tool-camel > ul > li input{
          flex: 1 1 0;
          width: 0;
      }
          `
      const input = wrap.querySelector('input');
      const ul = wrap.querySelector('ul');
      const ulinput = ul.querySelectorAll('input');
      function inputHandel (ev)  {
        if (ev.target === input) {
          const strs = camel(input.value);
          ulinput[0].value = strs.self;
          ulinput[1].value = strs.large;
          ulinput[2].value = strs.small;
          ulinput[3].value = strs._large;
          ulinput[4].value = strs._self;
        }
      }
      function ulinputhandel(ev){
        if ([].includes.call(ulinput, ev.target)) {
          ev.target.select();
          document.execCommand('copy')
        }
      }
      document.body.appendChild(wrap)
      document.body.appendChild(style)
      wrap.addEventListener('change', inputHandel);
      wrap.addEventListener('input', inputHandel);
      wrap.addEventListener('paste', inputHandel);
      wrap.addEventListener('click', ulinputhandel);
  }
});
